package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.CustomerDao;
import com.aaa.sbm.entity.Customer;
import com.aaa.sbm.service.CustomerService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerDao customerDao;

    @Override
    public Result getCustomerList(Page<Customer> page) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<Customer> customers = customerDao.selectList(page.getData());
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customers);
        return ResultUtil.success(customerPageInfo);
    }

    @Override
    public Result insertCustomer(Customer customer) {
        int insert = customerDao.insert(customer);
        return insert > 0 ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @Override
    public Result updateCustomer(Customer customer) {
        int update = customerDao.update(customer);
        return update > 0 ? ResultUtil.success("修改成功") : ResultUtil.fail("修改失败");
    }

    @Override
    public Result deleteCustomer(Integer id) {
        int delete = customerDao.deleteById(id);
        return delete > 0 ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @Override
    public Result listStatus() {
        List<Customer> customers = customerDao.selectByStatus();
        return ResultUtil.success(customers);
    }
}
